package com.ssm.helong.shop.controller;


import com.ssm.helong.shop.common.ServiceResultEnum;
import com.ssm.helong.shop.entity.ShoppingCar;

import com.ssm.helong.shop.service.ShoppingCarService;


import com.ssm.helong.shop.util.RedisUtils;
import com.ssm.helong.shop.util.Result;
import com.ssm.helong.shop.util.ResultGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.text.DateFormat;
import java.util.*;

@Controller
@RequestMapping("/shoppingCarApi")
public class ShoppingCarController {
    @Autowired
    private ShoppingCarService shoppingCarService;


    RedisUtils redisUtils = RedisUtils.getRedisUtil();

    /***
     * 添加商品到购物车
     * @param shoppingCar
     * @param user
     * @return: com.ssm.helong.shop.util.Result
     * @Author: helong.sun
     * @Date: 2021/4/24
     */
    @RequestMapping("/addShops")
    public Result saveShoppingCar(@RequestBody ShoppingCar shoppingCar, String user) {
        String token = redisUtils.get(user);
        if (token.equals(user)) {
            String temp = shoppingCarService.saveCartItem(shoppingCar);
            if (ServiceResultEnum.SUCCESS.getResult().equals(temp)) {
                return ResultGenerator.genSuccessResult();
            }
            //添加失败
            return ResultGenerator.genFailResult(temp);
        }
        return ResultGenerator.genFailResult("当前用户状态异常，添加失败");
    }

    /***
     * 修改购物车属性
     * @param shoppingCar
     * @param user
     * @return: com.ssm.helong.shop.util.Result
     * @Author: helong.sun
     * @Date: 2021/4/26
     */
    @PostMapping("/updateCar")
    @ResponseBody
    public Result updateShoppingCar(@RequestBody ShoppingCar shoppingCar, String user) {
        String token = redisUtils.get(user);
        if (token.equals(user)) {
            String temp = shoppingCarService.updateCartItem(shoppingCar);
            if (ServiceResultEnum.SUCCESS.getResult().equals(temp)) {
                return ResultGenerator.genSuccessResult();
            }
            //添加失败
            return ResultGenerator.genFailResult(temp);
        }
        return ResultGenerator.genFailResult("当前用户状态异常,修改失败");
    }

    /***
     * 删除购物车数据
     * @param cartItemId
     * @param user
     * @return: com.ssm.helong.shop.util.Result
     * @Author: helong.sun
     * @Date: 2021/4/26
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public Result delete(@RequestBody Integer cartItemId, String user) {
        String token = redisUtils.get(user);
        if (token.equals(user)) {
            Boolean temp = shoppingCarService.deleteById(cartItemId);
            if (temp) {
                return ResultGenerator.genSuccessResult();
            }
            //添加失败
            return ResultGenerator.genFailResult("未删除成功");
        }
        return ResultGenerator.genFailResult("当前用户状态异常,修改失败");
    }
}

